﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using OpenNETCF.IoC;
using OpenNETCF.MTConnect;
using MachineSimulator.Interfaces;
using System.Windows.Forms;

namespace MachineSimulator.Presenters
{
    public class MachinePresenter : INotifyPropertyChanged
    { 
        public event PropertyChangedEventHandler PropertyChanged;
        
        public IMachineInterface Machine { get; set; }

        [InjectionConstructor]
        public MachinePresenter([ServiceDependency]IMachineInterface machine)
        {
            Machine = machine;
            Machine.PropertyChanged += new PropertyChangedEventHandler(Machine_PropertyChanged);
        }

        public Control Invoker;

        void Machine_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            Invoker.InvokeIfRequired(c =>
                {
                    if (e.PropertyName == "X")
                    {
                        RaisePropertyChanged("XPos");
                    }
                    else if (e.PropertyName == "Y")
                    {
                        RaisePropertyChanged("YPos");
                    }
                    else
                    {
                        RaisePropertyChanged(e.PropertyName);
                    }
                });
        }

        public int XPos
        {
            get 
            {
                // note that we're getting the data from the Adapter.
                // We never hold it locally
                return this.Machine.X; 
            }
            internal set
            {
                // this sets the X value over in teh adapter (through the IoC/DI container)
                // which in turn publishes the data out of the MTConnect interface
                this.Machine.X = value;

                // this is for the data binding of the View UI to this value
                RaisePropertyChanged("XPos");
            }
        }

        public int YPos
        {
            get { return this.Machine.Y; }
            internal set
            {
                this.Machine.Y = value;
                RaisePropertyChanged("YPos");
            }
        }

        public int Power
        {
            get { return this.Machine.Power; }
            internal set
            {
                this.Machine.Power = value;
                RaisePropertyChanged("Power");
            }
        }

        public bool DoorOpen
        {
            get { return this.Machine.DoorOpen; }
            internal set
            {
                this.Machine.DoorOpen = value;
                RaisePropertyChanged("DoorOpen");
            }
        }

        public bool EStop
        {
            get { return this.Machine.EStop; }
            internal set
            {
                this.Machine.EStop = value;
                RaisePropertyChanged("EStop");
            }
        }

        private void RaisePropertyChanged(string property)
        {
            var handler = PropertyChanged;
            if (handler == null) return;

            handler(this, new PropertyChangedEventArgs(property));
        }
    }
}
